$(document).ready(function(){
	layui.use([ 'form', 'layedit', 'laydate' ], function() {
		var form = layui.form, layer = layui.layer, layedit = layui.layedit, laydate = layui.laydate;

		// 自定义验证规则
		form.verify({
			userName: function(value, dom){
			    if(!/(\w){6,12}/.test(value)){
			        return '用户名必须由数字、字母、下划线组成，长度为6~16位';
			    }
			    var message = '';
			    $.ajax({
			    	url: '/user/check/' + value,
			    	type: 'get',
			    	async: false,
			    	success: function(data){
			    		if(data.code){
			    			message = '该用户名已经存在'
			    		}
			    	}
			    });
			    if(message){
			    	return message;
			    }
			},
			userPass: [/(.+){6,16}$/, '密码不能有空格, 长度6~16位' ],
			userPassword: function(value, dom){
				if($('#userPass').val() != value){
					return "两次密码输入不一致";
				}
			},
			userMail:[/(.+){8,64}$/, '邮件地址至少8位, 最长64位']
		});

		// 监听提交
		form.on('submit(userForm)', function(data) {
			debugger
			$.ajax({
		    	url: '/user/register',
		    	type: 'post',
		    	data: data.field,
		    	success: function(data){
		    		if(!data.code){
		    			layer.alert('注册完成, 请登录', {
	    			      title: '提示'
	    			    }, function(){	    			    	
	    			    	window.location.href = '/';
	    			    })
		    		}else{
		    			layer.alert('注册失败', {
	    			      title: '提示'
	    			    })
		    		}
		    	}
		    });
		});
	});
});